package com.grechur.wanandroidcompose.page


import android.util.Log
import androidx.compose.foundation.gestures.scrollable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.grechur.wanandroidcompose.viewmodel.NavigationViewModel
import com.grechur.wanandroidcompose.widget.NaviItemLayout

@Composable
fun NavigationPage(globalNavHostController: NavHostController, modifier: Modifier = Modifier, navigationViewModel: NavigationViewModel = viewModel()) {
    DisposableEffect(Unit) {
        Log.e("zhouzhu","NavigationPage DisposableEffect")
        onDispose {
            Log.e("zhouzhu","NavigationPage onDispose")
        }
    }
    LaunchedEffect(Unit) {
        navigationViewModel.getNavigation()
    }
    val scrollState = rememberScrollState()
    navigationViewModel.navigationBeans.let { result ->
        if (result.isSuccess) {
            result.getOrDefault(listOf()).let { navigationBeans ->
                Column(modifier = Modifier
                    .padding(horizontal = 10.dp)
                    .navigationBarsPadding().padding(bottom = 56.dp).verticalScroll(state = scrollState)) {
                    navigationBeans.forEach {navigationBean->
                        NaviItemLayout(navigationBean = navigationBean, globalNavHostController = globalNavHostController)
                    }
                }
            }
        } else {

        }
    }
}

@Preview
@Composable
fun NavigationPagePreview() {
    NavigationPage(rememberNavController())
}

